package game.utils;

import game.frame.MainFrame;
import game.roles.bullets.*;
import game.roles.gamer.Gamer;
import game.roles.gamer.Helicopter;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @author liuyulai
 * Created with IntelliJ IDEA.
 * Date: 21.5.7
 * Time: 9:45
 * Description: 键盘事件处理类
 */
public class PressListenerOne extends PressBase implements KeyListener {

//    public static boolean isUp;
//    public static boolean isDown;
//    public static boolean isLeft;
//    public static boolean isRight;

    private int count;

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            isUp = true;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            isDown = true;
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            isLeft = true;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            isRight = true;
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            isUp = false;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            isDown = false;
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            isLeft = false;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            isRight = false;
        } else if (e.getKeyChar() == 'c') {
            releaseTanks();
        } else if (e.getKeyChar() == 'v') {
            releaseHelicopter();
        } else if (e.getKeyChar() == 'a' && MainFrame.sp == 100) {
            releaseSp();
        } else if (e.getKeyChar() == 'x') {
            if (Gamer.iceBullet) {
                bulletIce();
            } else {
                bulletFire();
            }
        } else if (e.getKeyChar() == 's') {
            upFire();
        } else if (e.getKeyChar() == 'z') {
            sendBooms();
        }
    }

    private void upFire() {
        MainFrame.roleList.add(new NormalBullet(MainFrame.gamer.x, MainFrame.gamer.y, NormalBullet.UP));
    }

    /**
     * 设置炸弹
     */
    private void sendBooms() {
        if (MainFrame.gamer.getStatues() == Gamer.GAMER_BACK || MainFrame.gamer.getStatues() == Gamer.GAMER_FRONT) {
            MainFrame.gamer.gamerSendBoom();
        }
    }


    /**
     * 设置子弹开火
     */
    protected void bulletFire() {
        if (MainFrame.gamer.getStatues() == Gamer.GAMER_FRONT) {
            //捡到特殊道具的情况下可以发射的弹幕
            if (Gamer.allowFireByThree) {
                MainFrame.roleList.add(new NormalBullet(MainFrame.gamer.x + 4, MainFrame.gamer.y + 1, GamerBullet.RIGHT_UP));
                MainFrame.roleList.add(new NormalBullet(MainFrame.gamer.x + 4, MainFrame.gamer.y + 1, GamerBullet.RIGHT_DOWN));
                count++;
                //按下100次后回复普通发射的效果
                if (count >= 100) {
                    Gamer.allowFireByThree = false;
                }
            }
            int x = MainFrame.gamer.x;
            int y = MainFrame.gamer.y;
            for (int i = 0; i < 3; i++) {
                MainFrame.roleList.add(new NormalBullet(x, y, NormalBullet.FRONT));
                x += 10;
                y += 10;
            }
        } else if (MainFrame.gamer.getStatues() == Gamer.GAMER_BACK) {
            if (Gamer.allowFireByThree) {
                MainFrame.roleList.add(new NormalBullet(MainFrame.gamer.x - 4, MainFrame.gamer.y + 1, GamerBullet.LEFT_UP));
                MainFrame.roleList.add(new NormalBullet(MainFrame.gamer.x - 4, MainFrame.gamer.y + 1, GamerBullet.LEFT_DOWN));
                count++;
                //按下15次后回复普通发射的效果
                if (count >= 100) {
                    Gamer.allowFireByThree = false;
                }
            }
            int x = MainFrame.gamer.x;
            int y = MainFrame.gamer.y;
            for (int i = 0; i < 3; i++) {
                MainFrame.roleList.add(new NormalBullet(x, y, GamerBullet.BACK));
                x -= 10;
                y += 10;
            }
        }
    }


    protected void bulletIce() {
        if (MainFrame.gamer.getStatues() == Gamer.GAMER_FRONT) {
            //捡到特殊道具的情况下可以发射的弹幕
            if (Gamer.allowFireByThree) {
                MainFrame.roleList.add(new IceBullet(MainFrame.gamer.x + 4, MainFrame.gamer.y + 1, GamerBullet.RIGHT_UP));
                MainFrame.roleList.add(new IceBullet(MainFrame.gamer.x + 4, MainFrame.gamer.y + 1, GamerBullet.RIGHT_DOWN));

                count++;
                //按下100次后回复普通发射的效果
                if (count >= 100) {
                    Gamer.allowFireByThree = false;
                }
            }
            int x = MainFrame.gamer.x;
            int y = MainFrame.gamer.y;
            for (int i = 0; i < 3; i++) {
                MainFrame.roleList.add(new IceBullet(x, y, GamerBullet.FRONT));
                x += 10;
                y += 10;
            }
        } else if (MainFrame.gamer.getStatues() == Gamer.GAMER_BACK) {
            if (Gamer.allowFireByThree) {
                MainFrame.roleList.add(new IceBullet(MainFrame.gamer.x - 4, MainFrame.gamer.y + 1, GamerBullet.LEFT_UP));
                MainFrame.roleList.add(new IceBullet(MainFrame.gamer.x - 4, MainFrame.gamer.y + 1, GamerBullet.LEFT_DOWN));
                count++;
                //按下15次后回复普通发射的效果
                if (count >= 100) {
                    Gamer.allowFireByThree = false;
                }
            }
            int x = MainFrame.gamer.x;
            int y = MainFrame.gamer.y;
            for (int i = 0; i < 3; i++) {
                MainFrame.roleList.add(new IceBullet(x, y, GamerBullet.BACK));
                x -= 10;
                y += 10;
            }
        }
    }


    /**
     * 积攒sp大招
     */
    protected void releaseSp() {
        MainFrame.sp = 0;
        int x = MainFrame.gamer.x;
        for (int i = 0; i < 9; i++) {
            MainFrame.roleList.add(new BigBoom(x, MainFrame.gamer.y));
            x += 200;
        }
    }

    /**
     * 直升机轰炸
     */
    private void releaseHelicopter() {
        if (Gamer.friendlyHelicopterNum > 0) {
            Gamer.friendlyHelicopterNum--;
            int x = 50;
            int y = -60;
            for (int i = 0; i < 9; i++) {
                MainFrame.roleList.add(new Helicopter(x, y));
                x += 200;
                y -= new Random().nextInt(20);
            }
        }
    }

    /**
     * 释放平地坦克
     */
    protected void releaseTanks() {
        if (Gamer.friendlyTankNum > 0) {
            MainFrame.roleList.add(new FriendlyTank(MainFrame.gamer.y));
            Gamer.friendlyTankNum--;
        }
    }
}
